package com.wu.wechat.util;

import com.wu.common.exception.WexinReqException;
import com.wu.common.utils.WeiXinConstant;
import com.wu.entity.weixin.po.MenuCreate;
import com.wu.entity.weixin.po.WeixinButton;
import com.wu.entity.weixin.requ.*;
import com.wu.common.handel.WeiXinReqService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import java.util.ArrayList;
import java.util.List;

public class JwMenuAPI {

    public JwMenuAPI() {
    }

    public static String createMenu(String accessToken, List<WeixinButton> button) throws WexinReqException {
        MenuCreate m = new MenuCreate();
        m.setAccess_token(accessToken);
        m.setButton(button);
        JSONObject result = WeiXinReqService.getInstance().doWeinxinReqJson(m);
        Object error = result.get(WeiXinConstant.RETURN_ERROR_INFO_CODE);
        String msg = "";
        if (error == null) {
            msg = result.getString("groupid");
        } else {
            msg = result.getString(WeiXinConstant.RETURN_ERROR_INFO_MSG);
        }

        return msg;
    }

    public static List<WeixinButton> getAllMenu(String accessToken) throws WexinReqException {
        MenuGet g = new MenuGet();
        g.setAccess_token(accessToken);
        JSONObject result = WeiXinReqService.getInstance().doWeinxinReqJson(g);
        Object error = result.get(WeiXinConstant.RETURN_ERROR_INFO_CODE);
        List<WeixinButton> lstButton = null;
        JSONObject menu = result.getJSONObject("menu");
        JSONArray buttons = menu.getJSONArray("button");
        JSONArray subButtons = null;
        lstButton = new ArrayList();
        WeixinButton btn = null;
        WeixinButton subBtn = null;
        List<WeixinButton> lstSubButton = null;

        for(int i = 0; i < buttons.size(); ++i) {
            btn = (WeixinButton)JSONObject.toBean(buttons.getJSONObject(i), WeixinButton.class);
            subButtons = buttons.getJSONObject(i).getJSONArray("sub_button");
            if (subButtons != null) {
                lstSubButton = new ArrayList();

                for(int j = 0; j < subButtons.size(); ++j) {
                    subBtn = (WeixinButton)JSONObject.toBean(subButtons.getJSONObject(j), WeixinButton.class);
                    lstSubButton.add(subBtn);
                }

                btn.setSub_button(lstSubButton);
            }

            lstButton.add(btn);
        }

        return lstButton;
    }


    public static CustomWeixinButtonConfig getAllMenuConfigure(String accessToken) throws WexinReqException {
        MenuConfigureGet cmcg = new MenuConfigureGet();
        cmcg.setAccess_token(accessToken);
        JSONObject result = WeiXinReqService.getInstance().doWeinxinReqJson(cmcg);
        Object error = result.get(WeiXinConstant.RETURN_ERROR_INFO_CODE);
        CustomWeixinButtonConfig customWeixinButtonConfig = (CustomWeixinButtonConfig)JSONObject.toBean(result, new CustomJsonConfig(CustomWeixinButtonConfig.class, "selfmenu_info"));
        JSONObject selfmenuInfo = result.getJSONObject("selfmenu_info");
        if (selfmenuInfo != null && !JSONUtils.isNull(selfmenuInfo)) {
            JSONArray buttons = selfmenuInfo.getJSONArray("button");
            List<WeixinButtonExtend> listButton = new ArrayList();

            for(int i = 0; i < buttons.size(); ++i) {
                WeixinButtonExtend weixinButtonExtend = (WeixinButtonExtend)JSONObject.toBean(buttons.getJSONObject(i), new CustomJsonConfig(WeixinButtonExtend.class, "sub_button"));
                JSONObject subButtonJsonObj = buttons.getJSONObject(i).getJSONObject("sub_button");
                if (subButtonJsonObj != null && !JSONUtils.isNull(subButtonJsonObj)) {
                    JSONArray subButtons = subButtonJsonObj.getJSONArray("list");
                    if (subButtons != null) {
                        List<WeixinButtonExtend> listSubButton = new ArrayList();
                        int j = 0;

                        while(true) {
                            if (j >= subButtons.size()) {
                                weixinButtonExtend.setSub_button(listSubButton);
                                break;
                            }

                            WeixinButtonExtend subBtn = (WeixinButtonExtend)JSONObject.toBean(subButtons.getJSONObject(j), new CustomJsonConfig(WeixinButtonExtend.class, "news_info"));
                            JSONObject newsInfoJsonObj = subButtons.getJSONObject(j).getJSONObject("news_info");
                            if (newsInfoJsonObj != null && !JSONUtils.isNull(newsInfoJsonObj)) {
                                JSONArray newsInfos = newsInfoJsonObj.getJSONArray("list");
                                List<WxArticleConfig> listNewsInfo = new ArrayList();

                                for(int k = 0; k < newsInfos.size(); ++k) {
                                    WxArticleConfig wxArticleConfig = (WxArticleConfig)JSONObject.toBean(newsInfos.getJSONObject(k), WxArticleConfig.class);
                                    listNewsInfo.add(wxArticleConfig);
                                }

                                subBtn.setNews_info(listNewsInfo);
                            }

                            listSubButton.add(subBtn);
                            ++j;
                        }
                    }
                }

                listButton.add(weixinButtonExtend);
            }

            customWeixinButtonConfig.setSelfmenu_info(listButton);
        }

        return customWeixinButtonConfig;
    }

    public static void main(String[] args) {
        String s = "";

        try {
            s = "3DGIfJqqupzTPxvq_P-0ATDC2MDjFLqaz8S41SPmRIqLaA3PSb8FgN_PuhpZ5jEB4D6w7ZNeX3gbC3CfSOAz2wt4DxVKi2HD5BCjoecrB0Q";
            System.out.println(s);
            CustomWeixinButtonConfig var2 = getAllMenuConfigure(s);
        } catch (WexinReqException var3) {
            var3.printStackTrace();
        }

    }
}
